//
//  GYDSendWBView.swift
//  Weibo
//
//  Created by 贵永冬 on 16/8/1.
//  Copyright © 2016年 贵永冬. All rights reserved.
//

import UIKit
import ReactiveCocoa
import SnapKit

class GYDSendWBView: UIView,UITextViewDelegate,UIScrollViewDelegate {

    var textView : UITextView?
    var placeholderLabel : UILabel?
    var toolBar : GYDToolBar?
    lazy var keyBoardView : GYDKeyboardView = {
        let keyView = GYDKeyboardView(frame: CGRect(x: 0, y: 0, width: GYDWindowWidth, height: 250))
        keyView.emoticonBlock = { [unowned self] (emoticon: GYDKeyboardEmoticon) -> () in
            self.parsingEmoticon(emoticon)
        }
        return keyView
    }()
    private var toolBarBottom : Constraint?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        createSubViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func createSubViews() {
        createToolBar()
        createTextView()
    }
    func createTextView() {
        let textView = UITextView()
        self.textView = textView
        textView.delegate = self
        textView.font = UIFont.systemFontOfSize(17)
        self.addSubview(textView)
        textView.snp_makeConstraints { (make) in
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.top.equalTo(0)
            make.bottom.equalTo((self.toolBar?.snp_top)!)
        }
        textView.rac_textSignal().subscribeNext { [unowned self] (x) in
            guard let text : String = x as? String else {
                return
            }
            if text.characters.count > 0 {
                self.placeholderLabel?.text = nil
            }else if text.characters.count == 0 {
                self.placeholderLabel?.text = "分享新鲜事..."
            }
        }
        textView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(GYDSendWBView.changeKeyboard)))
        
        let placeholderLabel = UILabel()
        textView.addSubview(placeholderLabel)
        placeholderLabel.snp_makeConstraints { (make) in
            make.left.equalTo(6)
            make.top.equalTo(10)
            make.width.equalTo(100)
            make.height.equalTo(15)
        }
        placeholderLabel.text = "分享新鲜事..."
        placeholderLabel.textColor = RGB(150, g: 150, b: 150)
        placeholderLabel.font = UIFont.systemFontOfSize(17)
        self.placeholderLabel = placeholderLabel
        
        
    }
    func createToolBar() {
        let toolBar = GYDToolBar(frame: CGRect(x: 0, y: 0, width: GYDWindowWidth, height: 44))
        self.toolBar = toolBar
        toolBar.backgroundColor = RGB(250, g: 250, b: 250)
        self.addSubview(toolBar)
        toolBar.snp_makeConstraints { (make) in
            make.left.right.equalTo(0)
            self.toolBarBottom = make.bottom.equalTo(0).constraint
            make.height.equalTo(44)
        }
        toolBar.toolBarBlock = { [unowned self] (index : Int) -> () in
            if index == 3 {
                self.changeKeyboard()
            }
        }
        //键盘出现
        NSNotificationCenter.defaultCenter().rac_addObserverForName(UIKeyboardWillShowNotification, object: nil).subscribeNext { [unowned self] (x) in
            guard let info :NSNotification = x as? NSNotification else {
                return
            }
            let time = info.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
            let keyFrame = info.userInfo![UIKeyboardFrameEndUserInfoKey]!.CGRectValue
            let curve = info.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! NSNumber
            UIView.animateWithDuration(time, animations: {
                UIView.setAnimationCurve(UIViewAnimationCurve(rawValue: curve.integerValue)!)
                self.toolBarBottom?.updateOffset(-(keyFrame.size.height))
                }, completion: { (stop) in
                    
            })
            
        }
        //键盘消失
        NSNotificationCenter.defaultCenter().rac_addObserverForName(UIKeyboardWillHideNotification, object: nil).subscribeNext { [unowned self] (x) in
            guard let info :NSNotification = x as? NSNotification else {
                return
            }
            let time = info.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
            UIView.animateWithDuration(time, animations: {
                self.toolBarBottom?.updateOffset(0)
                }, completion: { (stop) in
                    
            })
        }
    }
    
    private func parsingEmoticon(emoticon:GYDKeyboardEmoticon) {
        //删除
        if emoticon.isRemoveButton {
            
        }
        // emoticon表情
        if let tempEmostr = emoticon.emoticonStr {
            //取出光标的位置
            let range = self.textView?.selectedTextRange
            self.textView?.replaceRange(range!, withText: tempEmostr)
            return
        }
        //图片
        if  let tempPngpath = emoticon.pngPath {
            return
        }
    }
    
    
    @objc private func changeKeyboard() {
        textView?.resignFirstResponder()
        if textView?.inputView != nil {
            textView?.inputView = nil
        }else {
            textView?.inputView = keyBoardView
        }
        textView?.becomeFirstResponder()
    }
    
    
    //MARK: -UITextViewDelegate
    func scrollViewWillBeginDragging(scrollView: UIScrollView) {
        self.textView!.resignFirstResponder()
    }
    deinit {
        print("发送微博消失")
    }
    
    
}
